const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');
const dateTimePicker = require('../../utils/dateTimePicker.js');
var app = getApp();
Page({
  data: {
    startTime: null,
    endTime: null,
    userId: null,
    dateTimeArray1: null,
    dateTimeArray: null,
    startYear: 2020,
    endYear: 2050,
    hasLogin: false
  },

onLoad(){
    // 获取完整的年月日 时分秒，以及默认显示的数组
   var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
   var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
        dateTimeArray: obj1.dateTimeArray,
        dateTimeArray1: obj1.dateTimeArray,
        startTime: obj.dateTime,
        endTime: obj1.dateTime
    });
},


  onReady: function() {

  },
  onShow: function() {
    // 页面显示
    this.setData({
      hasLogin: app.globalData.hasLogin
    });

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
    
    changeStartDate(e) {
      this.setData({ startTime: e.detail.value });
      console.log("========"+this.data.dateTimeArray[0][this.data.startTime[0]])

      console.log("========"+this.data.dateTimeArray[1][this.data.startTime[1]])

      console.log("========"+this.data.dateTimeArray[2][this.data.startTime[2]]+" ")

      console.log("========"+this.data.dateTimeArray[3][this.data.startTime[3]]+":")

      console.log("========"+this.data.dateTimeArray[4][this.data.startTime[4]])

      console.log("========"+this.data.dateTimeArray[0][this.data.startTime[0]]+"-"+this.data.dateTimeArray[1][this.data.startTime[1]]+"-"+this.data.dateTimeArray[2][this.data.startTime[2]]+" "+this.data.dateTimeArray[3][this.data.startTime[3]]+":"+this.data.dateTimeArray[4][this.data.startTime[4]]+":00")
      //  this.setData({ startTime: this.data.dateTimeArray[0][this.data.startTime[0]]+"-"+this.data.dateTimeArray[1][this.data.startTime[1]]+"-"+this.data.dateTimeArray[2][this.data.startTime[2]]+" "+this.data.dateTimeArray[3][this.data.startTime[3]]+":"+this.data.dateTimeArray[4][this.data.startTime[4]]});

    },

    changeEndDate(e) {
      this.setData({ endTime: e.detail.value });
  },

    changeStartDateColumn(e) {
        var arr = this.data.startTime, dateArr = this.data.dateTimeArray;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        this.setData({
          dateTimeArray: dateArr
        });
    },

    changeEndDateColumn(e) {
      var arr = this.data.endTime, dateArr = this.data.dateTimeArray1;

      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

      this.setData({
          dateTimeArray1: dateArr
      });
  },

  submitduty: function() {
    var that = this;
    if (this.data.startTime.length < 1 || this.data.endTime.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入值班开始时间和值班结束时间',
        showCancel: false
      });
      return false;
    }
    console.log("sssss  "+that.data.startTime)

    wx.request({
      url: api.SubmitDuty,
      data: {
        startTime: this.data.dateTimeArray[0][this.data.startTime[0]]+"-"+this.data.dateTimeArray[1][this.data.startTime[1]]+"-"+this.data.dateTimeArray[2][this.data.startTime[2]]+" "+this.data.dateTimeArray[3][this.data.startTime[3]]+":"+this.data.dateTimeArray[4][this.data.startTime[4]]+":00",
        endTime: this.data.dateTimeArray1[0][this.data.endTime[0]]+"-"+this.data.dateTimeArray1[1][this.data.endTime[1]]+"-"+this.data.dateTimeArray1[2][this.data.endTime[2]]+" "+this.data.dateTimeArray1[3][this.data.endTime[3]]+":"+this.data.dateTimeArray1[4][this.data.endTime[4]]+":00",
        userId:wx.getStorageSync('userId')
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.errno == 0) {
          wx.showToast({
            title: '打卡成功',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showModal({
            title: '错误信息',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },

  bindEndTimeInput: function(e) {

    this.setData({
      endTime: e.detail.value
    });
  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})